import Pagination from '@/components/Pagination'
export default {
	data() {
		return {
			modal:false,
			modalTitle:"",
			show:false,
			request:{
				"pageNum":1,
				"pageSize":10,
				"username":null,
				"role":null
			},
			userData:{
				"username":"",
				"password":"",
				"role":null,
				"name":"",
				"email":"",
				"mobile":"",
				"status":"ENABLE"
			},
			users:[],
		}
	},
	computed: {
		
	},
	watch:{
		
	},
	methods: {
		search(){
			this.getUers();
		},
		edit(user){
			this.modalTitle = "修改用户";
			this.modal = true;
			this.userData = JSON.parse(JSON.stringify(user));
		},
		add(){
			this.modalTitle = "添加用户";
			this.modal = true;
			//重置
			this.userData = {
				"username":"",
				"password":"",
				"role":null,
				"name":"",
				"email":"",
				"mobile":"",
				"status":"ENABLE"
			}
		},
		modalOk(){
			let url = '/user/update';
			if(this.modalTitle == "添加用户"){
				url = '/user/create';
			}
			this.$http.post(url,this.userData)
				.then((res) => {
					if(res.data.code=='601'){
						this.$Message.success(`${this.modalTitle}成功！`);
						this.getUers();
						return;
					}
					this.$Notice.error({
	                    title: `${this.modalTitle}失败！`,
	                    desc: res.data.message
					});
				})
		},
		reset(user){			
			this.$http.post('/user/password/reset',user)
				.then((res) => {
					if(res.data.code=='601'){
						this.$Modal.success({
							title: "密码重置成功",
							content: "已重置密码为初始密码123456，请尽快修改密码！"
						});
						return;
					}
					this.$Notice.error({
	                    title: `密码重置失败！`,
	                    desc: res.data.message
					});
				})
		},
		getUers(){
			this.$http.post('/user/list',this.request)
				.then((res) => {
					if(res.data.code=='601'){
						this.show = true;
						this.users = res.data.data.list;
						if(this.users.length==0){
							this.show = false;
							this.$Notice.error({
								desc: "查询为空"
							});
						}
						return;
					}
					this.show = false;
					this.$Notice.error({
	                    title: '查询失败',
	                    desc: res.data.message
					});
				})
				.catch((err) => {
					//请求失败的处理
					this.show = false;
					this.$Notice.error({
	                    title: '查询失败',
	                    desc: err
	                });
				})
		}
	},
	components:{
		Pagination
	},
	created() {
		this.getUers();
	}
}